package com.yunfangdata.web.controller;


import com.yunfangdata.domain.PersonageSalary;
import com.yunfangdata.domain.Staff;
import com.yunfangdata.query.QueryObject;
import com.yunfangdata.query.StaffQueryObject;
import com.yunfangdata.service.IFamilyBackgroundService;
import com.yunfangdata.service.IPersonageSalaryService;
import com.yunfangdata.service.IStaffService;
import com.yunfangdata.service.IWorkExperienceService;
import com.yunfangdata.util.JsonResult;
import com.yunfangdata.util.PageResult;
import com.yunfangdata.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("staff")
public class StaffController {
    @Autowired
    private IStaffService staffService;
    @Autowired
    private IPersonageSalaryService PersonageSalaryService;
    @Autowired
    private IWorkExperienceService WorkExperienceService;


    @RequiresPermissions("staff:index")
    @PermissionName("工人首页")
    @RequestMapping("")
    public String index() {
        return "staff/list";
    }


    @RequiresPermissions("staff:list")
    @PermissionName("工人列表")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(StaffQueryObject qo) {
        return staffService.query(qo);
    }

    @RequiresPermissions("staff:saveOrUpdate")
    @PermissionName("工人更新或保存")
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Staff entity) {
        JsonResult jsonResult = new JsonResult();
        if (entity.getId() != null) {
            try {
                staffService.update(entity);
            } catch (Exception e) {
                e.printStackTrace();
                jsonResult = new JsonResult("更新失败");
            }
        } else {
            try {
                staffService.save(entity);
            } catch (Exception e) {
                e.printStackTrace();
                jsonResult = new JsonResult("保存失败");
            }
        }
        return jsonResult;
    }

    @RequiresPermissions("PersonageSalary:saveOrUpdate")
    @PermissionName("工资保存")
    @RequestMapping("PersonageSalary_saveOrUpdate")
    @ResponseBody
    public JsonResult PersonageSalary_saveOrUpdate(PersonageSalary entity,Long staffId) {
        JsonResult jsonResult = new JsonResult();
        if (entity.getId() != null) {
            try {
                PersonageSalaryService.update(entity, staffId);
            } catch (Exception e) {
                e.printStackTrace();
                jsonResult = new JsonResult("更新失败");
            }
        } else {
            try {
                PersonageSalaryService.save(entity,staffId);
            } catch (Exception e) {
                e.printStackTrace();
                jsonResult = new JsonResult("保存失败");
            }
        }
        return jsonResult;
    }


    @RequiresPermissions("staff:WorkExperienceList")
    @PermissionName("工作情况列表")
    @RequestMapping("WorkExperienceList")
    @ResponseBody
    public PageResult WorkExperienceList(QueryObject qo) {
        return WorkExperienceService.query(qo);
    }
}
